package com.zhuoyue.web.controller;

import com.zhuoyue.biz.core.api.domain.Geofencing;
import com.zhuoyue.biz.core.api.dto.rep.GeofencingRepDTO;
import com.zhuoyue.biz.core.api.dto.req.GeofencingReqDTO;
import com.zhuoyue.biz.core.api.service.impl.GeofencingService;
import com.zhuoyue.common.aop.auth.CheckLogin;
import com.zhuoyue.common.dto.ControllerResult;
import com.zhuoyue.common.group.ValidationGroups;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wjming
 * @ClassName: AppGeofencingController
 * @Description: 地理围栏信息配置
 * @Date 2021-07-05 19:48
 */
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/api/geofencing")
@Validated
public class AppGeofencingController {
    /**
     * 服务对象
     */
    private final GeofencingService tblGeofencingService;


    /**
     * 通过网关ID查询地理围栏信息
     *
     * @param gatewayId
     * @return
     */
    @GetMapping
    @CheckLogin
    public ControllerResult selectAll(@NotBlank(message = "网关Id不能为空") String gatewayId) {
        List<GeofencingRepDTO> geofencingReps = this.tblGeofencingService.getMessageList(gatewayId);
        return ControllerResult.toReturnDataSuccess("获取成功", geofencingReps);
    }


    /**
     * 新增数据
     *
     * @param geofencingReqDto
     * @return
     */
    @PostMapping
    @CheckLogin
    public ControllerResult insert(@Validated(ValidationGroups.Insert.class) @RequestBody List<GeofencingReqDTO> geofencingReqDto) {
        List<Geofencing> geofencingList = createGeofencingList(geofencingReqDto);
        List<GeofencingRepDTO> geofencingReps = this.tblGeofencingService.addMessage(geofencingList);
        return ControllerResult.toReturnDataSuccess("获取成功", geofencingReps);
    }

    /**
     * 修改数据
     *
     * @param geofencingReqDto
     * @return
     */
    @PutMapping
    @CheckLogin
    public ControllerResult update(@Validated(ValidationGroups.Update.class) @RequestBody List<GeofencingReqDTO> geofencingReqDto) {
        List<Geofencing> geofencingList = createGeofencingList(geofencingReqDto);
        List<GeofencingRepDTO> geofencingReps = this.tblGeofencingService.updateMessage(geofencingList);
        return ControllerResult.toReturnDataSuccess("修改成功", geofencingReps);
    }


    /**
     * 删除地理围栏设置数据
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @CheckLogin
    public ControllerResult delete(@RequestBody List<String> id) {
        this.tblGeofencingService.removeMessage(id);
        return ControllerResult.toReturnMessageSuccess("删除成功");
    }

    /**
     * 创建地理围栏数据层对象信息
     *
     * @param geofencingReqDto
     * @return
     */
    private List<Geofencing> createGeofencingList(List<GeofencingReqDTO> geofencingReqDto) {
        List<Geofencing> geofencings = new ArrayList<>(2);
        geofencings.add(new Geofencing().reqDtoToGeofencing(geofencingReqDto.get(0)));
        geofencings.add(new Geofencing().reqDtoToGeofencing(geofencingReqDto.get(1)));
        return geofencings;
    }
}
